package com.announcement.sevice.Impl;

import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import com.announcement.dao.MessageDao;
import com.announcement.entity.Message;
import com.announcement.entity.vo.Page;
import com.announcement.sevice.MessageService;

@Transactional
@Service
public class MessageServiceImpl implements MessageService {

	@Autowired
	private MessageDao messageDao;

	@Override
	public boolean addArticle(Message msg) {
		msg.setPublishDate(new Date(System.currentTimeMillis()));
		System.out.println(msg.getPublishDate());
		msg.setPublished(true);
		System.out.println(msg.getContent());
		System.out.println(HtmlUtils.htmlEscape(msg.getContent()));
		msg.setContent(HtmlUtils.htmlEscape(msg.getContent()));
		return messageDao.saveArticle(msg);
	}

	@Override
	public Page<Message> showArticleToHome(int pageIndex) {
		Page<Message> page = messageDao.showArticleToHome(pageIndex);
		return page;
	}

	@Override
	public boolean deleteArticle(int messId) {
		return messageDao.deleteArticle(messId);
	}

	@Override
	public Message showArticleById(int messId) {
		Message msg = messageDao.showArticleById(messId);
		msg.setContent(HtmlUtils.htmlUnescape(msg.getContent()));
		return msg;
	}

	@Override
	public Page<Message> showMyArticle(int pageIndex, int userId) {
		return messageDao.showMyArticle(pageIndex, userId);
	}

	@Override
	public boolean updateArticle(Message msg) {
		return messageDao.updateArticle(msg);
	}

	@Override
	public Page<Message> searchArticle(int pageIndex, String keywords) {
		// TODO Auto-generated method stub
		return messageDao.searchArticle(pageIndex, keywords);
	}

}
